﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace VietnameseLanguageProcessing
{
    public partial class ReplaceForm : Form
    {
        Hashtable lSentences;
        
        int countMatch;

        public ReplaceForm()
        {
            InitializeComponent();
            lSentences = new Hashtable();
            lbInfo.Text = "Ready";

            txtData.Text = Properties.Settings.Default.DataUrl;
            txtOutput.Text = Properties.Settings.Default.OutputUrl;
            txtPattIn.Text = Properties.Settings.Default.PatternIn;
            txtPattOut.Text = Properties.Settings.Default.PatternOut;
            cbExtract.Checked = Properties.Settings.Default.ExtractEnabled;
            cbFindSame.Checked = Properties.Settings.Default.FindSameEnabled;
            
        }

        private void process(string pattIn, string pattOut)
        {
            pbProcess.Value = 0;
            lbInfo.Text = "Processing";
            lvReplace.Items.Clear();
            ListViewItem lvi;
            countMatch = 0;
            for (int i = 0; i < lSentences.Count; i++)
            {
                if (Regex.IsMatch(lSentences[i].ToString(), pattIn))
                {
                    lvi = new ListViewItem(i + "");
                    lvi.SubItems.Add(lSentences[i].ToString());
                    lvi.SubItems.Add(getReplace(lSentences[i].ToString(), pattIn, pattOut));
                    lvReplace.Items.Add(lvi);
                    countMatch++;

                }
                pbProcess.Value = getProcessValue(lSentences.Count, i);
            }
            pbProcess.Value = 100;
            lbInfo.Text = "Process completed";
            txtInfo.Text = "Matches: " + countMatch;
        }
        private int getProcessValue(int full, int cur)
        {
            return cur * 100 / full;
        }
        private string getReplace(string input, string pattIn, string pattOut)
        {
            return Regex.Replace(input, pattIn, pattOut);
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            process(txtPattIn.Text, txtPattOut.Text);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            string line = "";
            System.IO.StreamReader sr = new System.IO.StreamReader(txtData.Text);
            lSentences.Clear();
            int i = 0;
            while ((line = sr.ReadLine()) != null)
                lSentences.Add(i++,line);
            MessageBox.Show("Read file completed");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            string temp = "";
            pbProcess.Value = 0;
            lbInfo.Text = "Replacing";
            int checkItem = 0;
            for (int i = 0; i < lvReplace.Items.Count; i++)
            {
                if (lvReplace.Items[i].Checked)
                {
                    checkItem++;
                    lSentences[int.Parse(lvReplace.Items[i].Text)] = lvReplace.Items[i].SubItems[2].Text;
                    temp += lvReplace.Items[i].Text+":"+lvReplace.Items[i].SubItems[1].Text + ":" + lvReplace.Items[i].SubItems[2].Text + "\n";
                }
                pbProcess.Value = getProcessValue(lvReplace.Items.Count, i);
            }
            pbProcess.Value = 100;
            lbInfo.Text = "Replace completed";
            txtInfo.Text += "\nMatches: " + countMatch + "\nReplaced: " + checkItem;
            txtInfo.ScrollToCaret();
            if (cbExtract.Checked)
            {
                StreamWriter sw = new StreamWriter(txtOutput.Text+"-"+Regex.Replace( DateTime.Now.ToString(),@"[^0-9]","") + ".txt");
                sw.Write(temp);
                sw.Close();
            }

        }

        private void btnWriteFile_Click(object sender, EventArgs e)
        {
            lbInfo.Text = "Writing file...";
            System.IO.StreamWriter sw = new System.IO.StreamWriter(txtOutput.Text);
            foreach (string item in lSentences)
            {
                sw.WriteLine(item);
            }
            lbInfo.Text = "Write completed";
            sw.Close();
        }


        private void lvReplace_ItemActivate(object sender, EventArgs e)
        {
            if (cbFindSame.Checked)
            {
                pbProcess.Value = 0;
                int count = 0;

                for (int i = 0; i < lvReplace.Items.Count; i++)
                {
                    if (Regex.Match(lvReplace.SelectedItems[0].SubItems[1].Text, txtPattIn.Text).Value == Regex.Match(lvReplace.Items[i].SubItems[1].Text, txtPattIn.Text).Value)
                    {
                        lvReplace.Items[i].Checked = lvReplace.SelectedItems[0].Checked;
                        count++;
                    }
                    pbProcess.Value = getProcessValue(lvReplace.Items.Count, i);
                }
                txtInfo.Text += "\nChecked: " + count;
            }
        }

        private void ReplaceForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.DataUrl=txtData.Text;
            Properties.Settings.Default.PatternIn=txtPattIn.Text;
            Properties.Settings.Default.PatternOut=txtPattOut.Text;
            Properties.Settings.Default.ExtractEnabled=cbExtract.Checked;
            Properties.Settings.Default.FindSameEnabled=cbFindSame.Checked;
            Properties.Settings.Default.OutputUrl = txtOutput.Text;
            Properties.Settings.Default.Save();
        }

        private void txtData_DoubleClick(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                txtData.Text = openFile.FileName;
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                string line = "";
                ListViewItem lvi;
                System.IO.StreamReader sr = new System.IO.StreamReader(openFile.FileName);
                lvReplace.Items.Clear();
                while ((line = sr.ReadLine()) != null)
                {
                    string[] linespt = line.Split(':');
                    lvi = new ListViewItem(linespt[0]);
                    lvi.SubItems.Add(linespt[2]);
                    lvi.SubItems.Add(linespt[1]);
                    lvReplace.Items.Add(lvi);
                }
                    
                MessageBox.Show("Loaded restore");
            }
        }

    }
}
